//
//  JWDialPadViewController.m
//  jinwang
//
//  Created by jzb on 2018/2/24.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWDialPadViewController.h"

@interface JWDialPadViewController ()

@property (nonatomic,strong)UIView *DialPadBGView;
@property (nonatomic,strong)UILabel *phoneNumLabel;
@property (nonatomic,strong)UIButton *removeBtn,*phoneBtn,*updownBtn;
@property (nonatomic,strong)NSString *phone;
@end

@implementation JWDialPadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _phone = @"";
    [self DialPadViewBuild];
}
- (void)DialPadViewBuild{
    
    _DialPadBGView = [[UIView alloc]initWithFrame:CGRM(0, 1334-800-49*2, 750, 800-150)];
    
    _DialPadBGView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_DialPadBGView];
    
    UIView *topLine = [[UIView alloc]initWithFrame:CGRM(0, 0, 750, 3*2)];
    topLine.backgroundColor = kRGBAColor(240, 240, 240, .5);
    [_DialPadBGView addSubview:topLine];
    
    _phoneNumLabel = [[UILabel alloc]initWithFrame:CGRM(2, 10, 650, 100)];
//    _phoneNumLabel.backgroundColor = [UIColor cyanColor];
    _phoneNumLabel.font = [UIFont fontWithName:@"PingFangSC-Light" size:25.f];
    _phoneNumLabel.textAlignment = NSTextAlignmentCenter;
    _phoneNumLabel.lineBreakMode = NSLineBreakByWordWrapping;
    [_DialPadBGView addSubview:_phoneNumLabel];
    
    _removeBtn = [[UIButton alloc]initWithFrame:CGRM(660, 40, 60, 40)];
//    _removeBtn.backgroundColor = [UIColor redColor];
//    [_removeBtn setImage:[UIImage imageNamed:@"jw_dialPad_removeIcon"] forState:UIControlStateNormal];
    [_removeBtn addTarget:self action:@selector(removeBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    UILongPressGestureRecognizer*longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(removeBtnLongOnclick:)];
    longPress.minimumPressDuration=1;//定义按的时间
    [_removeBtn addGestureRecognizer:longPress];
    [_removeBtn setBackgroundImage:[UIImage imageNamed:@"jw_dialPad_removeIcon"] forState:UIControlStateNormal];
    [_DialPadBGView addSubview:_removeBtn];
    
    UIView *butTopLine = [[UIView alloc]initWithFrame:CGRM(0, 110, 750, 2)];
    butTopLine.backgroundColor = kRGBAColor(230, 230, 230, 1);
    [_DialPadBGView addSubview:butTopLine];
//    _phoneBtn = [[UIButton alloc]initWithFrame:CGRM(670, 20, 40, 20)];
//    [_DialPadBGView addSubview:_phoneBtn];
    
    for (int i = 0; i<3; i++) {
        for (int j = 0; j<4; j++) {
            UIView *numberBGView = [[UIView alloc]initWithFrame:CGRM(750/3*i, (800-112-150)/4*j+112, 750/3, (800-112)/4)];
            [_DialPadBGView addSubview:numberBGView];
            UILabel *numberLab = [[UILabel alloc]initWithFrame:CGRM(0, 10, 750/3, (800-112-150)/4-10-40)];
            UILabel *numberLab00 = [[UILabel alloc]initWithFrame:CGRM(0, (800-112-150)/4-10-35, 750/3, 40)];
            if (i==0) {
                if (j==0) {
                    numberLab.text = @"1";
                }else if (j==1){
                    numberLab.text = @"4";
                    numberLab00.text = @"GHI";
                }else if (j==2){
                    numberLab.text = @"7";
                    numberLab00.text = @"PQRS";
                }else if (j==3){
                    numberLab.text = @"*";
                }
            }else if (i==1){
                if (j==0) {
                    numberLab.text = @"2";
                    numberLab00.text = @"ABC";
                }else if (j==1){
                    numberLab.text = @"5";
                    numberLab00.text = @"JKL";
                }else if (j==2){
                    numberLab.text = @"8";
                    numberLab00.text = @"TUV";
                }else if (j==3){
                    numberLab.text = @"0";
                    numberLab00.text = @"+";
                }
                
            }else if (i==2){
                if (j==0) {
                    numberLab.text = @"3";
                    numberLab00.text = @"EDF";
                }else if (j==1){
                    numberLab.text = @"6";
                    numberLab00.text = @"MNO";
                }else if (j==2){
                    numberLab.text = @"9";
                    numberLab00.text = @"WXYZ";
                }else if (j==3){
                    numberLab.text = @"#";
                }
                
            }
            numberLab.textAlignment = NSTextAlignmentCenter;
            numberLab.font = [UIFont fontWithName:@"PingFangSC-Light" size:35.f];
            numberLab00.font = [UIFont systemFontOfSize:13];
            numberLab00.textAlignment = NSTextAlignmentCenter;
            numberLab00.textColor = kRGBColor(100, 100, 100);
            [numberBGView addSubview:numberLab];
            [numberBGView addSubview:numberLab00];
            UIButton *btn = [[UIButton alloc]initWithFrame:numberBGView.bounds];
            btn.tag = (i+1)*10+(j+1);
            [btn addTarget:self action:@selector(numberBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
            [numberBGView addSubview:btn];
        }
    }
    _phoneBtn = [[UIButton alloc]initWithFrame:CGRM(750/2-50, 1334-125-49*2, 100, 100)];
//    _phoneBtn.backgroundColor = [UIColor cyanColor];
    [_phoneBtn setImage:[UIImage imageNamed:@"jw_bohaopan_phoneIcon"] forState:UIControlStateNormal];

   
    [_phoneBtn addTarget:self action:@selector(phoneBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_phoneBtn];
    _updownBtn = [[UIButton alloc]initWithFrame:CGRM(750/3/2-20, 1334-125-49*2+30, 40, 40)];
//    _updownBtn.backgroundColor = [UIColor cyanColor];
     [_updownBtn setImage:[UIImage imageNamed:@"jw_bohaopan_downIcon"] forState:UIControlStateNormal];
    [_updownBtn addTarget:self action:@selector(updownBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_updownBtn];
}
- (void)numberBtnOnclick:(UIButton *)sender{
    NSLog(@"-=-=-=>>%ld",sender.tag);
    NSString *str = @"";
    if (sender.tag == 11) {
        str = @"1";
    }else if (sender.tag == 21){
       str = @"2";
    }else if (sender.tag == 31){
       str = @"3";
    }else if (sender.tag == 12){
        str = @"4";
    }else if (sender.tag == 22){
        str = @"5";
    }else if (sender.tag == 32){
        str = @"6";
    }else if (sender.tag == 13){
        str = @"7";
    }else if (sender.tag == 23){
        str = @"8";
    }else if (sender.tag == 33){
        str = @"9";
    }else if (sender.tag == 14){
        str = @"*";
    }else if (sender.tag == 24){
        str = @"0";
    }else if (sender.tag == 34){
        str = @"#";
    }
    if (_phoneNumLabel.text.length > 0) {
        _phoneNumLabel.text = [NSString stringWithFormat:@"%@%@",_phoneNumLabel.text,str];
    }else{
        _phoneNumLabel.text = [NSString stringWithFormat:@"%@",str];
    }
}
- (void)removeBtnOnclick:(UIButton *)sender{
    
    if(_phoneNumLabel.text.length > 0){
        _phoneNumLabel.text = [_phoneNumLabel.text substringToIndex:([_phoneNumLabel.text length]-1)];// 去掉最后一个","
    }else{
        _phoneNumLabel.text = @"";
    }
}
- (void)removeBtnLongOnclick:(UIButton *)sender{

        _phoneNumLabel.text = @"";
}
- (void)phoneBtnOnclick:(UIButton *)sender{

    if ([_phone isEqualToString:@"0"]) {
        sender.selected = !sender.selected;
        _updownBtn.hidden = NO;
        _DialPadBGView.frame = CGRM(0, 1334-800-49*2, 750, 800-150);
        _phone = @"1";
//        sender.backgroundColor = [UIColor cyanColor];
        [_phoneBtn setImage:[UIImage imageNamed:@"jw_bohaopan_phoneIcon"] forState:UIControlStateNormal];
    }else{
        if (_phoneNumLabel.text.length > 0) {
            
            NSMutableString * phoneStr = [[NSMutableString alloc] initWithFormat:@"tel://%@",_phoneNumLabel.text];
            
            NSURL *phoneUrl = [NSURL URLWithString:phoneStr];
            
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                
                [[UIApplication sharedApplication] openURL:phoneUrl];
                
            });
        }
    }
}
- (void)updownBtnOnclick:(UIButton *)sender{
    sender.hidden = YES;
//    _phoneBtn.selected = NO;
    _phone = @"0";
//    _phoneBtn.backgroundColor = [UIColor purpleColor];
    [_phoneBtn setImage:[UIImage imageNamed:@"jw_bohaopan_phoneIcon_sel"] forState:UIControlStateNormal];
     _DialPadBGView.frame = CGRM(0, 1334, 750, 800-150);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
